#!/bin/bash


# This file is part of TeamTools.
#
# TeamTools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License V2 as published by
# the Free Software Foundation.
#
# TeamTools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License V2 for more details.
#
# You should have received a copy of the GNU General Public License
# along with TeamTools (see the LICENSE file)
#
# Copyright (2009 - 2014) - Mark Leeuw
 




export TTDIR=/usr/local/teamtools

cat <<!
+--------------------------------------------------------------+
|                                                              |
|                  TeamTools Batch installer                   |
|                                                              |
+--------------------------------------------------------------+

!

yum -y install coreutils

echo "Installing dependencies (via yum)"
yum -y install perl-CPAN perl-YAML perl-XML-Parser gcc-c++ gd-devel mod_perl make perl-Net-SSLeay perl-IO-Socket-SSL 
yum -y install apr-devel apr-util-devel sqlite-devel neon-devel

echo "Install and configure cpanm"
yes|cpan App::cpanminus

echo "Install Perl libraries (via CPAN)"
cpanm HTML::Template --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Error --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Net::SMTP::TLS --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm SVN::Client --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm SVN::Class --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm HTTP::Request::Common --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm JSON --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600 
cpanm LWP::UserAgent --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600 
cpanm XML::Simple --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600 
cpanm Devel::Trace --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600 
cpanm Data::Dumper --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600 

echo "Setting up directory structure"
mkdir -p /var/log/teamtools
mkdir -p /etc/teamtools

cat <<! >/etc/teamtools/batch.xml
<ToolConfig>
	<server>localhost</server>
	<username>admin</username>
	<password>admin</password>
	<repo>repo01</repo>
</ToolConfig>
!


export PERL5LIB=${TTDIR}/batch/lib
chmod 777 ${TTDIR}/batch/bin/*

>/var/log/teamtools/messages

echo "Set permissions (ownership, permissions and SELinux)"
yum -y install policycoreutils-python
chmod 755 /usr/local/teamtools/batch/bin/*

chcon -R --type httpd_sys_content_t /var/log/teamtools
chcon -R --type httpd_sys_content_t /etc/teamtools
chcon -R --type httpd_sys_content_t /usr/local/teamtools
semanage fcontext -a --type httpd_sys_content_t '/var/log/teamtools(/*)?'
semanage fcontext -a --type httpd_sys_content_t '/usr/local/teamtools(/*)?'
